﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.IO;
using System.Reflection;

/// <summary>
/// Summary description for CustomAdminControlVirtualFile
/// </summary>
public class CustomAdminControlVirtualFile : VirtualFile
{

    public CustomAdminControlVirtualFile(string virtualPath) : base(virtualPath)
    {
        path = VirtualPathUtility.ToAppRelative(virtualPath); 
    }

    string path;

    public override System.IO.Stream Open() 
    { 
        string[] parts = path.Split('/'); 
        string assemblyName = parts[2]; 
        string resourceName = parts[3]; 
        assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName); 
        System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(assemblyName);
        if (assembly != null)
        {
            ManifestResourceInfo info = assembly.GetManifestResourceInfo(resourceName);
            if (info == null)
            {
                throw new Exception(string.Format("Could not load the resource '{0}' from assembly '{1}'", resourceName, assembly.GetName()));
            }
            return assembly.GetManifestResourceStream(resourceName);
        }
        else
        {
            throw new Exception(string.Format("Could not load the assembly '{0}'", assemblyName));
        }
    } 

}
